package com.lyf.shop.services.Impl;

import com.lyf.shop.dao.Impl.UserDaoImpl;
import com.lyf.shop.dao.UserDao;
import com.lyf.shop.entity.User;
import com.lyf.shop.services.UserService;
import com.lyf.shop.util.MD5Utils;
import com.lyf.shop.util.StrUtils;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    @Override
    public User findUserByUname(String username) {
        try {
            if(StrUtils.empty(username)){
                return userDao.findUserByUname(username);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        try {
            if(user!=null){
                return userDao.addUser(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int activeUser(String email, String code) {
        try {
            //判断email  code 是否为空
            if(StrUtils.empty(email,code)){
                //通过email code 查询是否有对应用户存在
                User user = userDao.getUserByEmailAndCode(email,code);
               // System.out.println(userDao.updateUserStatus(user.getUid()));
                if(user!=null){//有这个用户
                    //获取查询到的用户的Id来进行一个激活的处理
                    if(user.getUstatus()==1){
                        return 2;
                    }else {
                        //给出响应  激活失败，激活成功,已经激活
                       int i = userDao.updateUserStatus(user.getUid());
                       if(i>0){
                           return 1;
                       }

                    }
                }
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<User> userList() {
        try {
            return userDao.userList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> userList(String uname,String usex) {
        try {
            return userDao.userList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteUser(int uid) {
        if (uid>0){
            try {
                return userDao.deleteUser(uid);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return 0;
    }
}
